/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrderBottom = function (root) {
  let front = 0, rear = 0
  let queue = [], res = [], temp = []
  let levelPos
  if (root != null) {
    queue[++rear] = root
  }
  levelPos = rear
  while (front < levelPos) {
    let p = queue[++front]
    temp.push(p.val)
    if (p.left) queue[++rear] = p.left
    if (p.right) queue[++rear] = p.right
    if (front == levelPos) {
      res.unshift(temp)
      levelPos = rear
      temp = []
    }
  }
  return res
}